package com.dminvoicemanager.reports;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.dminvoicemanager.DM_InvoiceMenu;
import com.dminvoicemanager.DM_Reports_Generator;
import com.dminvoicemanager.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class DM_Retailer_Discount_Report extends Activity {
	private static final String TAG = null;
	private String DBNAME = "dminvoicemanager.db";
	// Stock Loading
	private String TEMP_STOCK_LOADING_SUMMARY = "frepStockLoadingSummary";
	private String STOCK_LOAD_HEAD = "fLoadhed";
	private String STOCK_LOAD_DETAIL = "fLoadDet";
	private String USER_SETTINGS = "usersettings";
	String currentReportName, selectFromDate, selectToDate;
	TextView txtHeadingReports;
	String currentUserTemp, currentUserPrefix, UserLocCode,PrintvlaueFI,PrintcurrentDisVal;
	EditText dmreportfromdate, dmreporttodate, dmdisplayrepname;
	String TotalvlaueRecordRaw;
	String item_result_1 = "0", item_result_2 = "0", item_result_3 = "0",item_result_4 ="0",item_result_5 ="0",item_result_6 ="0";
	SimpleDateFormat originalFormat = new SimpleDateFormat("dd MM yyyy");
	SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");
	Date date, LoadingFromDate, LoadingToDate, LoadingOriDate;
	String DefaultDate = "01-01-1900";
	Double TotalInvDis = 0.0, TotalItemDis = 0.0,TotalDiscount = 0.0;
	int toDayTag = 0;

	public void BackToMainMenu(View view) {
		Intent myIntent = new Intent(view.getContext(), DM_Reports_Generator.class);
		myIntent.putExtra("TAGID", "RID/RNAME");
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivityForResult(myIntent, 0);
		finish();
		return;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_sales_summary_reportd);
		Bundle bundle = getIntent().getExtras();

		currentReportName = bundle.getString("REPORTNAME");
		selectFromDate = bundle.getString("FROMDATE");
		selectToDate = bundle.getString("TODATE");
	
		if(selectFromDate.equals(selectToDate)){
			toDayTag = 33;
			Log.v(TAG, "toDayTag:" + toDayTag);
		}
		else{
			try {
				date = originalFormat.parse(selectFromDate.replaceAll("[-+/.^:,]",
						" "));
				Log.v(TAG, "Old Format From:" + originalFormat.format(date));
				Log.v(TAG, "New Format From:" + targetFormat.format(date));
				selectFromDate = targetFormat.format(date);
				LoadingFromDate = (Date) targetFormat.parse(selectFromDate);

			} catch (ParseException ex) {

			}
			
			try {
				date = originalFormat.parse(selectToDate.replaceAll("[-+/.^:,]",
						" "));
				Log.v(TAG, "Old Format To:" + originalFormat.format(date));
				Log.v(TAG, "New Format To:" + targetFormat.format(date));
				selectToDate = targetFormat.format(date);
				LoadingToDate = (Date) targetFormat.parse(selectToDate);

			} catch (ParseException ex) {

			}
			
		}

		txtHeadingReports = (TextView) findViewById(R.id.txtHeadingReports);
		txtHeadingReports.setText(currentReportName);
		dmreportfromdate = (EditText) findViewById(R.id.dmreportfromdate);
		dmreportfromdate.setText(selectFromDate);
		dmreporttodate = (EditText) findViewById(R.id.dmreporttodate);
		dmreporttodate.setText(selectToDate);
		dmdisplayrepname = (EditText) findViewById(R.id.dmdisplayrepname);

		currentuserTemp();

		DisplayLoadingReport();
	}

	private void DisplayLoadingReport() {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		ListView lvtypedes = (ListView) findViewById(R.id.dminvoiceitemsdetlist);
		// create the grid item mapping
		String[] fromtrdes = new String[] { "item_result_1", "item_result_2",
				"item_result_3", "item_result_4", "item_result_5", "item_result_6" };

		int[] totrdes = new int[] { R.id.record_1, R.id.record_2,
				R.id.record_3, R.id.record_4, R.id.record_5 , R.id.record_6 };

		// prepare the list of all records
		List<HashMap<String, String>> fillMapstrdes = new ArrayList<HashMap<String, String>>();
		SQLiteDatabase mydb;
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor allrowitemsdes = null;
		String query;
		try {

			if(toDayTag == 33){
			query = "SELECT FD.invsysdate,FH.RefNo,FH.DebCode,FD.DisAmt,FH.InvDis,FH.TotalDis,DEB.DebName from FinvDet FD, FinvHed FH, fDebtor DEB Where FD.TxnDate = '"+ selectFromDate.trim()+ "' AND FH.RefNo = FD.RefNo AND FH.DebCode = DEB.DebCode group by FH.RefNo, FD.RefNo order by FH.RefNo";
			Log.v(TAG, "Old toDayTag To1:" + selectFromDate.trim());
			Log.v(TAG, "From Date:" + toDayTag);
			Log.v(TAG, "length:" + query.length());
			}
			else{
			query = "SELECT FD.invsysdate,FH.RefNo,FH.DebCode,FD.DisAmt,FH.InvDis,FH.TotalDis,DEB.DebName from FinvDet FD, FinvHed FH, fDebtor DEB Where invsysdate >= '"+ selectFromDate.trim()+ "' AND invsysdate <= '"+ selectToDate.trim() + "' AND FH.RefNo = FD.RefNo AND FH.DebCode = DEB.DebCode group by FH.RefNo, FD.RefNo order by FH.RefNo";
			Log.v(TAG, "Old toDayTag To1:" + selectFromDate.trim());	
			}
			//String query = "SELECT Itemcode,itemDescription,TxnType,DisAmt,CaseQty,PiceQty,Amt from FinvDet";
			allrowitemsdes = mydb.rawQuery(query, null);
		} catch (Exception e) {
		}

		if (allrowitemsdes != null) {
			if (allrowitemsdes.getCount() > 0) {
				while (allrowitemsdes.moveToNext()) {
						// invsysdate
						item_result_1 = allrowitemsdes.getString(0);
						// RefNo
						item_result_2 = allrowitemsdes.getString(1);
						// DebName
						item_result_3 = allrowitemsdes.getString(6);
						
						
						//TotalInvDis = 0.0, TotalItemDis = 0.0,TotalDiscount = 0.0;
						// InvDis 
						item_result_4 = allrowitemsdes.getString(4);
						double currentInvDis = Double.parseDouble(item_result_4);
						TotalInvDis = TotalInvDis +currentInvDis;
						// DisAmt 
						item_result_5 = allrowitemsdes.getString(3);
						double currentDisAmt = Double.parseDouble(item_result_5);
						TotalItemDis = TotalItemDis +currentDisAmt;
						// TotalDis 
						item_result_6 = allrowitemsdes.getString(5);
						double currentTotalDis = Double.parseDouble(item_result_6);
						TotalDiscount = TotalDiscount +currentTotalDis;

				
						HashMap<String, String> maptrdes = new HashMap<String, String>();
						maptrdes.put("item_result_1", item_result_1);
						maptrdes.put("item_result_2", item_result_2);
						maptrdes.put("item_result_3", item_result_3);
						maptrdes.put("item_result_4", item_result_4);
						maptrdes.put("item_result_5", item_result_5);
						maptrdes.put("item_result_6", item_result_6);
						// fill in the grid_item layout
						fillMapstrdes.add(maptrdes);
						SimpleAdapter adaptertrdes = new SimpleAdapter(this,
								fillMapstrdes,
								R.layout.dm_sales_summary_reportd_raw,
								fromtrdes, totrdes);
						lvtypedes.setAdapter(adaptertrdes);
				}
				// -----Print Stock Loading Summary--------
				HashMap<String, String> maptrdesem = new HashMap<String, String>();
				maptrdesem.put("item_result_1", "");
				maptrdesem.put("item_result_2", "");
				maptrdesem.put("item_result_3", "");
				maptrdesem.put("item_result_4", "");
				maptrdesem.put("item_result_5", "");
				maptrdesem.put("item_result_6", "");
				// fill in the grid_item layout
				fillMapstrdes.add(maptrdesem);
				SimpleAdapter adaptertrdesem = new SimpleAdapter(this,
						fillMapstrdes, R.layout.dm_sales_summary_reportd_raw,
						fromtrdes, totrdes);
				lvtypedes.setAdapter(adaptertrdesem);
				//TotalInvDis = 0.0, TotalItemDis = 0.0,TotalDiscount = 0.0;
				HashMap<String, String> maptrdessum = new HashMap<String, String>();
				String TotalInvDisStr = Double
						.toString(TotalInvDis);
				String TotalItemDisStr = Double
						.toString(TotalItemDis);
				String TotalDiscountStr = Double
						.toString(TotalDiscount);
				maptrdessum.put("item_result_1", "");
				maptrdessum.put("item_result_2", "");
				maptrdessum.put("item_result_3", "");
				maptrdessum.put("item_result_4", TotalInvDisStr);
				maptrdessum.put("item_result_5", TotalItemDisStr);
				maptrdessum.put("item_result_6", TotalDiscountStr);
				// fill in the grid_item layout
				fillMapstrdes.add(maptrdessum);
				SimpleAdapter adaptertrdessum = new SimpleAdapter(this,
						fillMapstrdes, R.layout.dm_sales_summary_reportd_raw,
						fromtrdes, totrdes);
				lvtypedes.setAdapter(adaptertrdessum);

			}
		}
		allrowitemsdes.close();
		mydb.close();
	}

	private void currentuserTemp() {
		// TODO Auto-generated method stub
		SQLiteDatabase mydb;
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor selectCurrentuserrec = null;
		try {
			selectCurrentuserrec = mydb.query(USER_SETTINGS, null, null, null,
					null, null, null);
		} catch (Exception e) {
		}
		if (selectCurrentuserrec != null) {
			if (selectCurrentuserrec.getCount() > 0) {
				while (selectCurrentuserrec.moveToNext()) {
					currentUserTemp = selectCurrentuserrec.getString(1);
					currentUserPrefix = selectCurrentuserrec.getString(2);
					UserLocCode = selectCurrentuserrec.getString(5);
					dmdisplayrepname.setText(currentUserTemp);
				}
			}
		}
		selectCurrentuserrec.close();
		mydb.close();
	}
	@Override
	public void onBackPressed() {
		// Do Here what ever you want do on back press;
	}
}
